<?php
/**
 * Created by PhpStorm.
 * User: adminyj
 * Date: 2019/2/17
 * Time: 5:38
 */

namespace app\api\controller;


use app\common\controller\Base;
use think\Db;

class Work extends Base
{


    /**
     * 就业岗位申请
     * */
    public function applyWork()
    {

        $id = input('id');
        $token = input('token');
        $name = input('name');
        $mobile = input('mobile');
        $info = input('info');

        if(!$id || !$token|| !$name|| !$mobile|| !$info){
            $this->result('','-1','数据获取有误','json');
        }

        $userInfo = $this->check($token);

        $where[] = ['user_id','=',$userInfo['uid']];
        $where[] = ['work_id','=',$id];

        $count = Db::name('apply_work')->where($where)->count();

        if($count > 0){
            $this->result('','-1','申请失败，请勿重复申请','json');
        }

            $data = [
                'user_id' => $userInfo['uid'],
                'work_id' => $id,
                'name' => $name,
                'mobile' => $mobile,
                'info' => $info,
                'create_time' => time()
            ];
            $res = Db::name('apply_work')->insert($data);

        if($res){
            $this->result('','1','申请成功，请根据岗位要求联系指定的电话或者邮箱。','json');
        }else{
            $this->result('','-1','申请失败','json');
        }


    }/**
     * 就业岗位收藏
     * */
    public function collectionWork()
    {

        $id = input('id');
        $token = input('token');

        if(!$id || !$token){
            $this->result('','-1','数据获取有误','json');
        }

        $userInfo = $this->check($token);

        $where[] = ['user_id','=',$userInfo['uid']];
        $where[] = ['work_id','=',$id];
        $msg = "收藏";

        $isCollection = Db::name('collection_work')->where($where)->value('status');
        $count = Db::name('collection_work')->where($where)->count();

        if($count > 0){
            if($isCollection == 1){
                $data['status'] = 0;
                $res = Db::name('collection_work')->where($where)->update($data);
                $msg = "取消收藏";
            }else if ($isCollection == 0){
                $data['status'] = 1;
                $res = Db::name('collection_work')->where($where)->update($data);
                $msg = "收藏";
            }
        }else{
            $msg = "收藏";
            $data = [
                'user_id' => $userInfo['uid'],
                'work_id' => $id,
                'status' => 1
            ];
            $res = Db::name('collection_work')->insert($data);
        }

        if($res){
            $this->result('','1',$msg.'成功','json');
        }else{
            $this->result('','-1',$msg.'失败','json');
        }


    }


}